package org.ran.demo.security.hash;

import org.ran.demo.util.ByteUtil;

public class ModularDigester implements Digester{
    private int divisor;
    public ModularDigester(int divisor) {
        if (divisor<1) {
            throw new IllegalArgumentException("divisor "+divisor+" is negative");
        }
        this.divisor=divisor;
    }

    @Override
    public byte[] digest(byte[] input) {
        int r=0;
        for (int i = 0; i < input.length; i++) {
            byte current = input[i];
            r = ((r << 3)+current)/this.divisor;
        }
        return ByteUtil.intToBytes(r);
    }

    @Override
    public int fixedLength() {
        return 1;
    }
    
}
